import Vue from 'vue'
import App from './App'

import pageHead from './components/page-head.vue'
import pageFoot from './components/page-foot.vue'
import uLink from '@/components/uLink.vue'
import store from './store'
import jquery from "jquery";
import {siteUrl} from'@/common/version.js'

Vue.prototype.$ = jquery;
Vue.config.productionTip = false

Vue.prototype.$store = store
Vue.prototype.$backgroundAudioData = {
	playing: false,
	playTime: 0,
	formatedPlayTime: '00:00:00'
}

Vue.prototype.sendRequest= function(param){
    var _self = this, 
        url = param.url,
        data = param.data || {}, 
        header = param.header|| {'content-type' : "application/json"},
        token = "";
        
    //拼接完整请求地址
    var requestUrl = this.siteBaseUrl + url;
    // //固定参数
    // if(!data.token){//如果参数中无token(除了小程序第一次通过code获取token的接口默认参数token = login,其他接口token参数都是在本地缓存中获取)
    //     token = uni.getStorageSync(this.sessionKey);
    //     if(!token){//本地无token需重新登录
    //         _self.login(backpage, backtype);
    //         return;
    //     }else{
    //         data.token = token;
    //     }
    // }
    // var timestamp = Date.parse(new Date());//时间戳
    // data["timestamp"] = timestamp;
    // data["device"] = "wxapp";//data["device"] = "iosapp";
    // data["ver"] = "1.0.0";//data["ver"] = "1.0.0";
    
    //GET或POST
    if(param.method){
        param.method = param.method.toUpperCase();//小写改为大写
    }
    
    //网络请求
    return new Promise((resolver,reject)=>{uni.request({
			url: requestUrl,
			method: param.method || "GET",
			header: header || {'content-type' : "application/json"},
			data: data,
			success: res => {
				if (res.statusCode && res.statusCode != 200) {//api错误
					
					uni.showModal({
						content:"网络或服务器错误，请检查网络"
					});
					return;
				}
				resolver(res.data)
				
				
				typeof param.success == "function" && param.success(res.data);
			},
			fail: (e) => {
				uni.showModal({
					content:"网络或服务器错误，请检查网络" 
				});
				
				typeof param.fail == "function" && param.fail(res.data);
			}
		})
	})
}



Vue.prototype.siteBaseUrl = siteUrl.BaseUrl;


Vue.component('page-head', pageHead)
Vue.component('page-foot', pageFoot)
Vue.component('uLink', uLink)

App.mpType = 'app'

const app = new Vue({
	store,
	...App
})
app.$mount()
